// Copyright (c) 2020 Presto Labs Pte. Ltd.
// Author: chunhui

#pragma once

#include <cstdint>
#include <limits>

namespace interval_feed {

class VwapCalculator {
 public:
  VwapCalculator() {
    pq_sum_ = 0;
    q_sum_ = 0;
  }

  double GetPqSum() const { return pq_sum_; }
  double GetQSum() const { return q_sum_; }

  void Update(double price, double qty) {
    pq_sum_ += price * qty;
    q_sum_ += qty;
  }

  double GetVwap() const {
    if (q_sum_ == 0) {
      return std::numeric_limits<double>::quiet_NaN();
    }
    return GetPqSum() / GetQSum();
  }

 private:
  double pq_sum_;
  double q_sum_;
};

}  // namespace interval_feed
